package cn.iocoder.yudao.module.system.util.a_gaojian;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class FileUtil extends cn.hutool.core.io.FileUtil {

    public static byte[] toByteArray(InputStream inputStream) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int n = 0;
        while (-1 != (n = inputStream.read(buffer))){
            outputStream.write(buffer,0,n);
        }
        return outputStream.toByteArray();
    }

    public static String getExt(String fileName) {
        if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
            return fileName.substring(fileName.lastIndexOf(".") + 1);
        } else {
            return "";
        }
    }

    /**
     * 查看当前路径是否存在
     *
     * @param filePath
     */
    public static Boolean isExistsFile(String filePath) {
        File file = new File(filePath);
        return file.exists();
    }
    /**
     * 查看当前路径是否存在没有创建
     *
     * @param filePath
     */
    public static void createFile(String filePath) {
        File file = new File(filePath);
            file.mkdirs();

    }
    public static String getFileSize(Long size) {
        double length = Double.valueOf(String.valueOf(size));
        //如果字节数少于1024，则直接以B为单位，否则先除于1024，后3位因太少无意义
        if (length < 1024) {
            return length + "B";
        } else {
            length = length / 1024.0;
        }
        //如果原字节数除于1024之后，少于1024，则可以直接以KB作为单位
        //因为还没有到达要使用另一个单位的时候
        //接下去以此类推
        if (length < 1024) {
            long round = Math.round(length * 100);
            String size2 = round / 100.0 + "KB";
            return size2;
            //  return Math.round(length * 100) / 100.0 + "KB";
        } else {
            length = length / 1024.0;
        }
        if (length < 1024) {
            //因为如果以MB为单位的话，要保留最后1位小数，
            //因此，把此数乘以100之后再取余
            return Math.round(length * 100) / 100.0 + "MB";
        } else {
            //否则如果要以GB为单位的，先除于1024再作同样的处理
            return Math.round(length / 1024 * 100) / 100.0 + "GB";
        }
    }

    /**
     * 判斷是否是图片
     *
     * @param imageSuffix:后缀名称
     * @param imageSuffixes:后缀名称数组
     * @return
     */
    public static Boolean isorNotImageSuffixe(String imageSuffix, String[] imageSuffixes) {
        for (String suffix : imageSuffixes) {
            if (suffix.equals(imageSuffix)) {
                return true;
            }
        }
        return false;
    }

    /**
     *  判断是否是word类型
     * @param docName
     * @param docmentSuffixs
     * @return
     */
    public static Boolean isorNotWordSuffixe(String docName,String[] docmentSuffixs){
        for (String s : docmentSuffixs) {
            if (s.equals(docName)){
                return true;
            }
        }
        return false;
    }
    /**
     * // 视频类
     * const videoSufix= ["avi","wmv","mpg","mpeg","mov","rm","ram","swf","flv","mp4","wma","rm","rmvb","flv","mpg","mkv"]
     * // 音频
     * const audioSuffix = ["wav","midi","cda","mp3",,"m4a","ape","flac","ogg","aif","aiff","mid","wma","ra","vqf"]
     * // 压缩文件
     * const zipSuffix = ["rar","zip","arj","z"]
     * // 图片
     * const imgSuffix = ['jpg', 'jpeg', 'gif', 'png' ,'bmp','webp']
     */
    /**
     * @param suffixName       上传的附件后缀名称
     * @param videoSuffix    视频后缀字符串 以英文 ";" 分号分割
     * @param imageSuffix    图片后置字符串 以英文 ";" 分号分隔
     * @param attachmentSuffix    其他 以英文 ";" 分号分割
     * @return
     */
    public static Integer getAttachmentType(String suffixName, String videoSuffix,String attachmentSuffix, String imageSuffix) {
        Integer number = null;
        // 判断是不是视频文件
        String[] videoSuffixs = videoSuffix.split(";");
        for (String suffix : videoSuffixs) {
            if (suffix.equals(suffixName)) {
                number = 1;
                return number;
            }
        }
        // 判断是否是图片
        String[] imageSuffixs = imageSuffix.split(";");
        for (String suffix : imageSuffixs) {
            if (suffix.equals(suffixName)){
                number = 2;
                return number;
            }
        }
        String[] attachmentSuffixs = attachmentSuffix.split(";");
        for (String suffix : attachmentSuffixs) {
            if (suffix.equals(suffixName)){
                number = 3;
                return number;
            }
        }
        return number;
    }

    public static boolean isChinese(char c) {
        // 根据Unicode编码判断字符是否为中文字符
        return Character.UnicodeScript.of(c) == Character.UnicodeScript.HAN;
    }

    /**
     * 字符串中特殊字符处理
     */
    public static String specificString(String str){
        return str.replaceAll("[^0-9a-zA-Z\\.]", "_");
    }

    public static void main(String[] args) {
        String thumb = cn.hutool.core.io.FileUtil.getParent("D:\\upload\\video\\11.mp4",1);
        System.out.println(thumb);
    }
}
